import axios from 'axios';
import {serialize} from '@/util/util';
import {getToken} from '@/util/auth';
import website from '@/config/website';
import {Base64} from 'js-base64';
import 'nprogress/nprogress.css';

export default function down(url,method,data,onProgress=null){
  const instance = axios.create({
    url:url,
    timeout: 30 * 60 * 1000,
    withCredentials: true,
    onUploadProgress: onProgress,
    responseType: 'blob'
  });
  let option={}
  let header = {Authorization:`Basic ${Base64.encode(`${website.clientId}:${website.clientSecret}`)}`,'Blade-Auth':'bearer ' + getToken()}
  option.headers = { ...header}
  option.method = method
  method=='get'?option.params = {...data}:option.data = data;
  instance.interceptors.request.use((config) => {
    // 在发送请求之前做些什么
    // console.log(config)
    return config
  }, (error) => {
    // 对请求错误做些什么
    console.log(error)
    return Promise.resolve(error.response)
  });

  // 添加响应拦截器
  instance.interceptors.response.use(response => {
    // 对响应数据做点什么
    return response
  }, error => {
    // 对响应错误做点什么
    console.log(error)
    return Promise.resolve(error.response)
  });
  return instance(option)
}
